%% PARAMS
clear all
opt=0;
SECTOR_ID=[24;29;527]
% case opt

%opt=1;
%SECTOR_ID =9;


%% Chosing stocks in the same sector and equivalent liquidity
all_sec_id_ok = getLiquidSecurityInSector(opt,SECTOR_ID);

%% Get financial ratios from selected stocks
sub_sec_id_ok = all_sec_id_ok(:,1);
[financial_ratio, dataX] = getFIFromSecurities(sub_sec_id_ok);

%% Normalize data X
dataX_normalize = normalizeDataX(dataX);
%%
[mat_X, vec_y, TotalData, dataX_normalize,pricevec] = preparePriceData(dataX_normalize);
[b,~,~,inmodel] = stepwisefit(mat_X,vec_y);
b=b';
YFIT = b(inmodel)*mat_X(:,inmodel)';

%%
vec_y = vec_y';
residual = vec_y-YFIT;
[predResidual, vpredResidual] = predictprice_kalman(residual);

plot(residual, '-go'); 
hold on;
plot(predResidual,'-k*');
legend('Residual', 'Predicted Residual');

onewaycost = 0;
optC1 = FindOptimizedC(predResidual,vpredResidual,residual);
[alpha1_position,alpha_ind] = alphaindicator(predResidual, vpredResidual,residual,optC1);


%% 
Testchunk=chunk(dataX_normalize(:,3),dataX_normalize(:,1));
window=0;
mat_x=[];
mat_y=[];
pos1=1;
n = length(Testchunk);
for i=1:length(Testchunk)-1
    fprintf('Processing %d / %d\n',i, n)
    pos2=sum(dataX_normalize(1:Testchunk(i+1)-1,end));
    [xvariable yvariable]=PrepareRegressX(TotalData(pos1+...
        (isnan(TotalData(pos1,1))):pos2,:),YFIT(Testchunk(i):Testchunk(i+1)-1),...
        dataX_normalize(Testchunk(i):Testchunk(i+1)-1,end));
    pos1=pos2+1;
    mat_x=[mat_x;xvariable];
    mat_y=[mat_y;yvariable];
    %     window=[window;[size(xvariable,1) length(yvariable)]];
end


%% Start to use neural network to predict
% [mat_x mat_y]=checkoutliner(mat_x,mat_y);
% mat_x=mat_x(:,[11:end-2 end]);
% mat_x = normalizeDataX(mat_x);
% %Use neural network to predict 
% inputs = mat_x';
% targets = mat_y';
% hiddenLayerSize = 10;
% net = fitnet(hiddenLayerSize);
% net.divideParam.trainRatio = 70/100;
% net.divideParam.valRatio = 15/100;
% net.divideParam.testRatio = 15/100;
% 
% [net,tr] = train(net,inputs,targets);
% 
% outputs = net(inputs);
% errors = gsubtract(targets,outputs);
% performance = perform(net,targets,outputs);



